package tr.gov.tubitak.bilgem.esya.certselector;

import javax.swing.*;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.util.Enumeration;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 11.10.2011
 * Time: 09:07
 * To change this template use File | Settings | File Templates.
 */
public class TreeUtil
{
    private TreeUtil(){};
    public static void expandTree(JTree tree, boolean expand)
    {
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        expandAll(tree, new TreePath(root), expand);
    }

    public static void expandAll(JTree tree, TreePath path, boolean expand)
    {
        TreeNode node = (TreeNode) path.getLastPathComponent();
       if (node.getChildCount() >= 0)
       {
           Enumeration enumeration = node.children();
           while (enumeration.hasMoreElements())
           {
                TreeNode ns = (TreeNode) enumeration.nextElement();
                TreePath ps = path.pathByAddingChild(ns);
                expandAll(tree, ps, expand);
           }
        }
        if (expand)
        {
            tree.expandPath(path);
        } else
        {
            tree.collapsePath(path);
        }
    }
}
